package deprecated.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import domain.dao.interfaces.IDAOContact;
import domain.entities.impl.Contact;
import domain.entities.impl.User;
import domain.entities.interfaces.IContact;
import util.DomainUtils;
import util.FormArgumentsException;

/**
 * Servlet implementation class SearchContactServlet
 */
public class SearchContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SearchContactServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String keywords = DomainUtils.trim(request.getParameter("keywords"));
		
		ApplicationContext context = util.SpringUtils.getApplicationContext();

		try {
			checks(request, keywords);
			IDAOContact daoContact = (IDAOContact) context.getBean("beanDAOContact");
			List<IContact> contacts = daoContact.searchContact(keywords.split(" "));
					
//			List<Contact> contacts = 
//					DAOFactory.getDaoContact().searchContact(keywords.split(" "));
			request.setAttribute(DomainUtils.SEARCH_CONTACT_RESULT, contacts);

			DomainUtils.displayInfoMessage(
					request, 
					DomainUtils.SHOW_CONTACT_ALERT_CURRENT_COLOR, 
					"The research has finished successfully.");
		}
		catch(Exception e) {

			if((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
			}
		}
		

		request.setAttribute("keywords", keywords);
		request.getRequestDispatcher("pages/showAllContact.jsp").forward(request, response);
	}

	private void checks(HttpServletRequest request, String keywords) throws Exception {

		HttpSession session = request.getSession(false);

		
		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}
		
		if (keywords == null || keywords.isEmpty()) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"The field \"Search\" should not be empty");
			throw new FormArgumentsException();
		}
	}

}
